import Vue from 'vue'
import Router from 'vue-router'
import NProgress from 'nprogress' // Progress 进度条
import 'nprogress/nprogress.css'// Progress 进度条样式
import { getToken } from '@/utils/cookie.js'
import { Notification } from "element-ui";


import Layout from '../../src/views/Layout.vue'

import { testRouter } from './stage/test'
Vue.use(Router)
let router = new Router({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    // ---------------------------NavBar 路由菜单---------------------------
    // ---------------------------菜单必须有children------------------------
    {
      path: '/',
      name: 'home',
      title: '平台总览',
      component: Layout,
      redirect: '/plat',
      children: [
        {
          path: '/plat',
          name: 'plat',
          title: '平台总览',
          component: () => import('@/views/Plat/Plat.vue')
        },
      ]
    },
    ...testRouter,
    // ---------------------------非菜单---------------------------
    {
      path: '/login',
      name: 'login',
      hideInMenu: true,
      component: () => import(/* webpackChunkName: "login" */ '@/views/Login.vue')
    },
  ]
})


/**
 * @路由守卫函数
 */
const login_page = '/login'   //登录页设置
const home_page = '/'         //首页设置

router.beforeEach((to, from, next) => {
  NProgress.start()
  if (getToken()) {
    if (to.path === login_page) {
      next({ path: home_page })
      NProgress.done()
    } else {
      next()
    }
  } else {
    if (to.path === login_page) {
      next()
    } else {
      Notification.error({
        title: '提示',
        message: '请您先登陆',
        duration: 3000
      });
      next({ path: login_page })
      NProgress.done()
    }
  }
})
router.afterEach((to, from) => {
  NProgress.done()
})


//路由导出
export default router;